import Types from '../../action/types';

const defaultState = {};

/**
 * popular: {
 *      java:{
 *             items:[],
 *             isLoading:false
 *      },
 *      ios:{
 *             items:[],
 *             isLoading:false
 *      }
 * }
 * 0.stata树横向扩展
 * 1.如何动态的设置store，和动态获取store(难点：storekey不固定)
 * 这是一个 reducer，形式为 (state, action) => state 的纯函数。
 * 描述了 action 如何把 state 转变成下一个 state。
 *
 * state 的形式取决于你，可以是基本类型、数组、对象,
 * 当 state 变化时需要返回全新的对象，而不是修改传入的参数。
 *
 * 下面例子使用 `switch` 语句和字符串来做判断，但你可以写帮助类(helper)
 * 根据不同的约定（如方法映射）来判断，只要适用你的项目即可。
 */
export default function onAction(state = defaultState, action) {
  switch (action.type) {
    case Types.TRENDING_REFRSH_SUCCESS: //下拉刷新成功
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          items: action.items, //原始数据
          projectModes: action.projectModes, //此次要展示的数据
          hideLoadingMore: false,
          isLoading: false,
          pageIndex: action.pageIndex,
        },
      };
    case Types.TRENDING_REFRSH: //下拉刷新
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          hideLoadingMore: true,
          isLoading: true,
        },
      };
    case Types.TRENDING_REFRSH_FAIL: //下拉刷新失败
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          isLoading: false,
        },
      };
    case Types.TRENDING_LOAD_MORE_SUCCESS: //上拉加载更多成功
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          projectModes: action.projectModes,
          hideLoadingMore: false,
          pageIndex: action.pageIndex,
        },
      };
    case Types.TRENDING_LOAD_MORE_FAI: //上拉加载更多失败
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          hideLoadingMore: true,
          pageIndex: action.pageIndex,
        },
      };
    case Types.FLUSH_TRENDING_FAVORITE: //刷新收藏状态
      return {
        ...state,
        [action.storeName]: {
          ...state[action.storeName],
          projectModes: action.projectModes,
        },
      };
    default:
      return state;
  }
}
